package com.fbinggun.fbinggunutil.plugin.nbt;

import de.tr7zw.nbtapi.NBTBlock;
import de.tr7zw.nbtapi.NBTCompound;
import de.tr7zw.nbtapi.NBTTileEntity;
import org.bukkit.block.Block;

/**
 * NBTAPI 插件Block API
 */
public class BlockNBT {


    public static String getString(String key, Block block){
        NBTCompound nbt = new NBTBlock(block).getData();
        System.out.println(nbt);
        if(key.contains(".")){
            String[] args = key.split(".");
            if(args.length==1){
                return   nbt.getString(key);
            }else {
                NBTCompound nbtCompound= nbt.getCompound(args[0]);
                if(nbtCompound==null){
                    return null;
                }
                for(int x =0;x<=args.length;x++){
                    if(x==0){
                        continue;
                    }
                    String k = args[x];
                    if(k.equals(args[args.length-1])) {
                        return  nbtCompound.getString(args[args.length-1]);
                    }
                    nbtCompound=nbtCompound.getCompound(args[x]);
                }
                return null;
            }
        }else {
            return  nbt.getString(key);
        }
    }

    public static Double getDouble(String key,Block block){
        NBTCompound nbt = new NBTBlock(block).getData();
        if(key.contains(".")){
            String[] args = key.split(".");
            if(args.length==1){
                return   nbt.getDouble(key);
            }else {
                NBTCompound nbtCompound=nbt.getCompound(args[0]);
                if(nbtCompound==null){
                    return 0d;
                }
                for(int x =0;x<=args.length;x++){
                    if(x==0){
                        continue;
                    }
                    String k = args[x];
                    if(k.equals(args[args.length-1])) {
                        return  nbtCompound.getDouble(args[args.length-1]);
                    }
                    nbtCompound=nbtCompound.getCompound(args[x]);
                }
                return 0d;
            }
        }else {
            return nbt.getDouble(key);
        }
    }
    public static int getInt(String key,Block block){
        NBTCompound nbt = new NBTBlock(block).getData();
        if(key.contains(".")){
            String[] args = key.split(".");
            if(args.length==1){
                return   nbt.getInteger(key);
            }else {
                NBTCompound nbtCompound=nbt.getCompound(args[0]);
                if(nbtCompound==null){
                    return 0;
                }
                for(int x =0;x<=args.length;x++){
                    if(x==0){
                        continue;
                    }
                    String k = args[x];
                    if(k.equals(args[args.length-1])) {
                        return  nbtCompound.getInteger(args[args.length-1]);
                    }
                    nbtCompound=nbtCompound.getCompound(args[x]);
                }
                return 0;
            }
        }else {
            return nbt.getInteger(key);
        }
    }
    public static Long getLong(String key,Block block){
        NBTCompound nbt = new NBTBlock(block).getData();
        if(key.contains(".")){
            String[] args = key.split(".");
            if(args.length==1){
                return   nbt.getLong(key);
            }else {
                NBTCompound nbtCompound=nbt.getCompound(args[0]);
                if(nbtCompound==null){
                    return 0l;
                }
                for(int x =0;x<=args.length;x++){
                    if(x==0){
                        continue;
                    }
                    String k = args[x];
                    if(k.equals(args[args.length-1])) {
                        return  nbtCompound.getLong(args[args.length-1]);
                    }
                    nbtCompound=nbtCompound.getCompound(args[x]);
                }
                return 0l;
            }
        }else {
            return nbt.getLong(key);
        }
    }
    public static String to(Block block){
        NBTCompound nbt = new NBTBlock(block).getData();
        return nbt.toString();
    }
    public static String to2(Block block){
        NBTTileEntity tileEntity = new NBTTileEntity(block.getState());
        NBTCompound nbt = new NBTBlock(block).getData();
        return tileEntity.toString();
    }

}

